<template>
    <transition>
        <el-dialog :title="dialogType === 'add' ? '添加教材信息' : '修改教材信息'" v-model="showFlag" width="600px" append-to-body
            @close="handleClose" @open="handleOpen">
            <el-form hide-required-asterisk ref="formRef" :rules="formRule" :model="formInfo" label-width="auto"
                style="padding:30px 30px 0 30px">
                <el-form-item label="课程名称" prop="courseId">
                    <el-select placeholder="课程名称" v-model="formInfo.courseId">
                        <el-option v-for="e in courses" :key="e" :value="e.courseId"
                            :label="`${e.courseName} - ${e.className}`"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="标准书号" prop="isbn">
                    <el-input placeholder="标准书号" type="text" v-model="formInfo.isbn"></el-input>
                </el-form-item>
                <el-form-item label="教材名称" prop="name">
                    <el-input placeholder="教材名称" type="text" v-model="formInfo.name"></el-input>
                </el-form-item>
                <el-form-item label="出版社" prop="publisher">
                    <el-input placeholder="出版社" type="text" v-model="formInfo.publisher"></el-input>
                </el-form-item>
                <el-form-item label="编者" prop="author">
                    <el-input placeholder="编者" type="text" v-model="formInfo.author"></el-input>
                </el-form-item>
                <el-form-item label="估价" prop="price">
                    <el-input placeholder="估价" type="text" v-model="formInfo.price"></el-input>
                </el-form-item>
                <el-form-item label="出版时间" prop="publishDate">
                    <el-input placeholder="出版时间" type="text" v-model="formInfo.publishDate"></el-input>
                </el-form-item>
                <el-form-item label="班级" prop="classId">
                    <el-select placeholder="班级" v-model="formInfo.classId">
                        <el-option v-for="e in classes" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-top: 40px; margin-left: 372px; ">
                    <el-button @click="handleClose" size="mini">取消</el-button>
                    <el-button @click="formSubmit" size="mini" type="primary">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </transition>
</template>

<script setup>
import api from '@/api/api';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref, watch } from 'vue';

const props = defineProps({
    dialogType: {
        type: String,
        default: 'add'
    }, showDialog: {
        type: Boolean
    },
    applyId: {
        default: ''
    },
    editForm: {
        type: Object
    }
})
const emit = defineEmits(['dialogClosed'])
const showFlag = ref(false)

const courses = ref([
    // courseId, courseName, className
])
const classes = ref([
    // id, name
])

const formRef = ref(null)

const formRule = ref({
    isbn: [
        { required: true, message: '请填写标准书号', trigger: 'blur' }
    ],
    courseId: [
        { required: true, message: '请填写课程名称', trigger: 'blur' }
    ],
    name: [
        { required: true, message: '请填写教材名称', trigger: 'blur' }
    ],
    publisher: [
        { required: true, message: '请填写出版社', trigger: 'blur' }
    ],
    author: [
        { required: true, message: '请填写编者', trigger: 'blur' }
    ],
    price: [
        { required: true, message: '请填写估价', trigger: 'blur' }
    ],
    publishDate: [
        { required: true, message: '请填写出版时间', trigger: 'blur' }
    ],
    classId: [
        { required: true, message: '请填写班级', trigger: 'blur' }
    ],
})
const formInfo = reactive(
    {
        'isbn': '', 'name': '', 'publisher': '', 'author': '', 'price': '',
        'courseId': '', 'classId': '', 'publishDate': '',
    },
)
const formSubmit = () => {
    formRef.value.validate((valid) => {
        if (valid) {
            if (props.dialogType == 'add') {
                api.textbook.add(
                    formInfo.isbn, formInfo.name, formInfo.publisher,
                    formInfo.author, formInfo.price, formInfo.courseId, formInfo.classId,
                    formInfo.publishDate
                ).then((res) => {
                    ElMessage({
                        message: '添加成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            } else {
                api.textbook.edit(
                    props.applyId, formInfo.isbn, formInfo.name, formInfo.publisher,
                    formInfo.author, formInfo.price, formInfo.courseId, formInfo.classId,
                    formInfo.publishDate
                ).then((res) => {
                    ElMessage({
                        message: '修改成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            }

        }
    })
}
const handleClose = () => {
    showFlag.value = false
    formRef.value.resetFields()
    emit('dialogClosed')
}
const handleOpen = () => {
    if (props.dialogType==='edit'){
        formInfo.isbn=props.editForm.isbn
        formInfo.courseId=props.editForm.courseId
        formInfo.name=props.editForm.name
        formInfo.publisher=props.editForm.publisher
        formInfo.author=props.editForm.author
        formInfo.price=props.editForm.price
        formInfo.publishDate=props.editForm.publishDate
        formInfo.classId=props.editForm.classId
    }
}

watch(() => props.showDialog, (newValue, oldValue) => {
    showFlag.value = props.showDialog
})


onMounted(() => {
    api.scheduled_course.get_all().then((res) => {
        courses.value = res.data.data
    }).catch((err) => {
        console.log(err)
    })
    api.class_js.get_all().then((res) => {
        classes.value = res.data.data
    }).catch((err) => {
        console.log(err)
    })
})
</script>